package com.xxx.reggie.controller;

import com.xxx.reggie.common.R;
import com.xxx.reggie.dto.SetmealDto;
import com.xxx.reggie.entity.Employee;
import com.xxx.reggie.entity.Page;
import com.xxx.reggie.entity.Setmeal;
import com.xxx.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session) {
        //接收数据封装到SetmealDto
        //完善套餐数据(更新人,创建人)
        Employee employee = (Employee) session.getAttribute("employee");
        setmealDto.setCreateUser(employee.getId());
        setmealDto.setUpdateUser(employee.getId());

        //调用业务实现添加套餐
        setmealService.save(setmealDto);

        //封装返回
        return R.success("添加成功");
    }


    @GetMapping("/page")
    public R<Page<SetmealDto>> findByName(Integer page, Integer pageSize, String name) {

        //接收三个参数
        //调用业务层获取分页Page<Setmeal>
        Page<SetmealDto> setmealPage = setmealService.findByName(page, pageSize, name);

        return R.success(setmealPage);
    }


    /**
     * 作用：批量删除
     *
     * @param ids 要删除套餐的id
     * @return
     */
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids,HttpSession session){

        Employee employee = (Employee) session.getAttribute("employee");


        setmealService.deleteByIds(ids,employee.getId());

        return R.success("删除成功");
    }


    @PostMapping("/status/{status}")
    public R<String> switchSetmeal(@RequestParam List<Long> ids, @PathVariable Integer status){

        setmealService.switchSetmeal(ids,status);

        return R.success("修改成功");
    }

    @GetMapping("list")
    public R<List<Setmeal>> list(Long categoryId,Integer status) {
       List<Setmeal> setmealList = setmealService.findAllByCidAndStatus(categoryId,status);

        return R.success(setmealList);
    }
}
